package com.wa.my.util;

import cn.hutool.core.util.StrUtil;

public class WebSocketUtil {

    public final static String COOKIE_HEADER_NAME = "cookie";

    public final static String TOKEN_PREFIX = "sessionID=";

    /** 从str中获取sessionID=值
     * @param cookieHeader
     * @return
     */
    public static String getHttpRequestToken(String cookieHeader) {
        if (StrUtil.isNotEmpty(cookieHeader) && cookieHeader.contains(WebSocketUtil.TOKEN_PREFIX)) {
            int i = 0;
            String token = null;
            String[] cookies = cookieHeader.split(";");
            while (i < cookies.length) {
                String cookie = cookies[i++];
                if (StrUtil.isNotEmpty(cookie) && cookie.trim().startsWith(TOKEN_PREFIX)) {
                    token = cookie.substring(cookie.indexOf(TOKEN_PREFIX) + TOKEN_PREFIX.length());
                    break;
                }
            }
            return token;
        }
        return null;
    }

}
